package com.yb.project.system.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import com.yb.common.utils.SnowflakeIdWorker;
import com.yb.framework.security.utils.SecurityUtil;
import com.yb.project.base.mapper.BaseMapper;
import com.yb.project.base.service.impl.BaseServiceImpl;
import com.yb.project.system.domain.SysUser;
import com.yb.project.system.domain.SysUserRole;
import com.yb.project.system.mapper.SysUserMapper;
import com.yb.project.system.mapper.SysUserRoleMapper;
import com.yb.project.system.service.SysUserService;

@Service
public class SysUserServiceImpl extends BaseServiceImpl<SysUser> implements SysUserService {
	
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;
	
	@Override
	protected BaseMapper<SysUser> getMapper() {
		return sysUserMapper;
	}

	@Override
	public SysUser getByUsername(String username) {
		// TODO Auto-generated method stub
		return sysUserMapper.getByUsername(username);
	}

	@Override
	public List<String> getRolesByUsername(String username) {
		// TODO Auto-generated method stub
		return sysUserMapper.getRolesByUsername(username);
	}

	@Override
	public List<String> getPermissionsByUsername(String username) {
		// TODO Auto-generated method stub
		return sysUserMapper.getPermissionsByUsername(username);
	}

	@Override
	public List<String> getAllPermissions() {
		// TODO Auto-generated method stub
		return sysUserMapper.getAllPermissions();
	}

	@Override
	public void insertUser(SysUser sysUser) {
		// TODO Auto-generated method stub
		// 添加用户信息
		long id = SnowflakeIdWorker.getInstance().getId();
		sysUser.setId(id);
		sysUser.setCreateBy(SecurityUtil.getUsername());
		sysUser.setCreateTime(new Date());
		sysUser.setPassword(new BCryptPasswordEncoder().encode(sysUser.getPassword()));
		sysUserMapper.insert(sysUser);
		// 添加用户角色关联
		Long[] roleIds = sysUser.getRoleIds();
		if(roleIds != null && roleIds.length > 0) {
			ArrayList<SysUserRole> sysUserRoleList = new ArrayList<SysUserRole>();
			for (Long roleId : roleIds) {
				SysUserRole sysUserRole = new SysUserRole();
				sysUserRole.setUserId(id);
				sysUserRole.setRoleId(roleId);
				sysUserRoleList.add(sysUserRole);
			}
			sysUserRoleMapper.insertList(sysUserRoleList);
		}
	}

	@Override
	public void updateUser(SysUser sysUser) {
		// TODO Auto-generated method stub
		// 更新用户信息
		sysUser.setUpdateBy(SecurityUtil.getUsername());
		sysUser.setUpdateTime(new Date());
		sysUserMapper.update(sysUser);
		
		// 更新用户角色关联（先删除，再添加）
		sysUserRoleMapper.deleteByUserId(sysUser.getId());
		Long[] roleIds = sysUser.getRoleIds();
		if(roleIds != null && roleIds.length > 0) {
			ArrayList<SysUserRole> sysUserRoleList = new ArrayList<SysUserRole>();
			for (Long roleId : roleIds) {
				SysUserRole sysUserRole = new SysUserRole();
				sysUserRole.setUserId(sysUser.getId());
				sysUserRole.setRoleId(roleId);
				sysUserRoleList.add(sysUserRole);
			}
			sysUserRoleMapper.insertList(sysUserRoleList);
		}
		
		
	}
}